package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//    }
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage1(String msg) throws InterruptedException {
        System.out.println("spring 消费者1接收到消息：【" + msg + "】");
        Thread.sleep(25);
    }
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage2(String msg) throws InterruptedException {
        System.err.println("spring 消费者2接收到消息：【" + msg + "】");
        Thread.sleep(100);
    }
    @RabbitListener(queues = "fanout.queue1")
    public void FanoutQueueMessage1(String msg) throws InterruptedException {
        System.out.println("Fanout.queue1接收到消息：【" + msg + "】");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void FanoutQueueMessage2(String msg) throws InterruptedException {
        System.err.println("Fanout.queue2接收到消息：【" + msg + "】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void DirectQueueMessage1(String msg) throws InterruptedException {
        System.out.println("Direct.queue1接收到消息：【" + msg + "】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void DirectQueueMessage2(String msg) throws InterruptedException {
        System.err.println("Direct.queue2接收到消息：【" + msg + "】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "topic.direct", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void TopicQueueMessage1(String msg) throws InterruptedException {
        System.err.println("Direct.queue2接收到消息：【" + msg + "】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "topic.direct", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void TopicQueueMessage2(String msg) throws InterruptedException {
        System.err.println("Direct.queue2接收到消息：【" + msg + "】");
    }
    @RabbitListener(queues = "object.queue")
    public void listenObiectQueueMessage1(Map map) throws InterruptedException {
        System.out.println("spring 消费者1接收到消息：【" + map + "】");
        Thread.sleep(25);
    }
}
